<?php
header('Content-Type: text/plain');
echo "PHP环境检查:\n\n";

// 检查必需扩展
$required_extensions = [
    'PDO',
    'pdo_mysql',
    'mbstring',
    'json'
];

echo "扩展检查:\n";
foreach ($required_extensions as $ext) {
    $loaded = extension_loaded($ext);
    echo "$ext: " . ($loaded ? "已启用" : "未启用") . "\n";
}

// 检查PHP版本
echo "\nPHP版本: " . PHP_VERSION . "\n";

// 检查自动加载
echo "\n自动加载检查:\n";
$autoloadPath = __DIR__ . '/../vendor/autoload.php';
if (file_exists($autoloadPath)) {
    require_once $autoloadPath;
    echo "vendor/autoload.php 加载成功\n";
    
    // 检查Dotenv类是否存在
    if (class_exists('Dotenv\Dotenv')) {
        echo "Dotenv类已加载\n";
    } else {
        echo "Dotenv类未找到\n";
    }
} else {
    echo "vendor/autoload.php 未找到 - 请运行 'composer install'\n";
    exit;
}

// 检查数据库连接
echo "\n数据库连接测试:\n";
try {
    $dbConfig = [
        'host' => 'localhost',
        'dbname' => 'php_blog_system',
        'username' => 'root',
        'password' => 'root',
        'charset' => 'utf8mb4'
    ];
    
    $dsn = "mysql:host={$dbConfig['host']};dbname={$dbConfig['dbname']};charset={$dbConfig['charset']}";
    $db = new PDO($dsn, $dbConfig['username'], $dbConfig['password']);
    echo "数据库连接成功!\n";
    
    // 检查表是否存在
    $tables = $db->query("SHOW TABLES")->fetchAll(PDO::FETCH_COLUMN);
    echo "数据库表: " . implode(', ', $tables) . "\n";
} catch (Exception $e) {
    echo "数据库连接失败: " . $e->getMessage() . "\n";
}
?>
